$(function() {
	financialFund = 1;
	getFinancialFundData()

})

function getFinancialFundData() {
	getWord()
	const currYear = localStorage.getItem("year");
	const currMon = localStorage.getItem("month");
	$.get(
	  `${API_URL}getdata?budgetYear=${currYear}&budgetMonth=${currMon}&status=2&chartPage=605&chartNo=2&username=${getUsername}`,
	  {},
	  function (res) {
		  console.log(res)
		  let re = eval("(" + res + ")");
		  console.log(re)
		  getOverall(re.items[0].charts[0].data)
	  }
	)
	$.get(
	  `${API_URL}getdata?budgetYear=${currYear}&budgetMonth=${currMon}&status=2&chartPage=605&chartNo=3&username=${getUsername}`,
	  {},
	  function (res) {
		  console.log(res)
		  let re = eval("(" + res + ")");
		  console.log(re)
		  getBasic(re.items[0].charts[0].data)
	  }
	)
	$.get(
	  `${API_URL}getdata?budgetYear=${currYear}&budgetMonth=${currMon}&status=2&chartPage=605&chartNo=4&username=${getUsername}`,
	  {},
	  function (res) {
		  console.log(res)
		  let re = eval("(" + res + ")");
		  console.log(re)
		  getExpend(re.items[0].charts[0].data)
	  }
	)
	$.get(
	  `${API_URL}getdata?budgetYear=${currYear}&budgetMonth=${currMon}&status=2&chartPage=605&chartNo=5&username=${getUsername}`,
	  {},
	  function (res) {
		  console.log(res)
		  let re = eval("(" + res + ")");
		  console.log(re)
		  let data = re.items[0].charts[0].data;
		  let id = 0;
		  let parentData = [];
		  for(let i=0;i<data.length;i++){
			  if(data[i].key01 == ''){
				  ++ id;
				  data[i].id = id ;
				  data[i].pid = 0;
				  parentData.push(data[i])
			  }
			  if(data[i].key03 == ''){
				  data[i].key03 = '-'
			  }
		  }
		  for(let i=0;i<parentData.length;i++){
			  for(let j=0;j<data.length;j++){
				  if(parentData[i].key02 == data[j].key01){
					  data[j].pid = parentData[i].id
				  }
			  }
		  }
		  getSchedule(data)
	  }
	)
	
}

function getWord(){
	const currYear = localStorage.getItem("year");
	const currMon = localStorage.getItem("month");
	$.get(
	  `${API_URL}getdata?budgetYear=${currYear}&budgetMonth=${currMon}&status=2&chartPage=605&chartNo=1&username=${getUsername}`,
	  {},
	  function (res) {
		  console.log(res)
		  let re = eval("(" + res + ")");
		  console.log(re)
		  let data = re.items[0].charts[0].data[0]
		  $('.word1').html(`<div class="icon">
							<img src="assets/images/financial-fund/icon_Income.svg" >
						</div>
						<div class="text">
							<p>本月来款总额</p>
							<h4>${FormatWYCash(data.key01)}<span>万元</span></h4>
						</div>`)
		  $('.word2').html(`<div class="icon">
							<img src="assets/images/financial-fund/icon_buget.svg" >
						</div>
						<div class="text">
							<p>本月预算总额</p>
							<h4>${FormatWYCash(data.key02)}<span>万元</span></h4>
						</div>`)
		  $('.word3').html(`<div class="icon">
							<img src="assets/images/financial-fund/icon_expend.svg" >
						</div>
						<div class="text">
							<p>本月支出总额</p>
							<h4>${FormatWYCash(data.key03)}<span> 万元</span></h4>
						</div>`)
		$('.word4').html(`<div class="">
									<p>本月基本支出总额</p>
									<h2>${FormatWYCash(data.key04)}<span>万元</span></h2>
									<div class="contrast">
										<div class="${data.key05>0?'rise':data.key05==0?'fair':'drop'}">
											<img src="${data.key05>0?'assets/images/departmental-charge/arrow-up-filling.svg':data.key05==0?'assets/images/departmental-charge/icon-pingjun@2x.png':'assets/images/departmental-charge/arrow-down-filling.svg'}" >
											<span>${(data.key05 * 100).toFixed(1)}%</span>
										</div>
										<p>较上月</p>
									</div>
								</div> `)
		$('.word5').html(`<div class="">
									<p>本月项目支出总额</p>
									<h2>${FormatWYCash(data.key06)}<span>万元</span></h2>
									<div class="contrast">
										<div class="${data.key07>0?'rise':data.key07==0?'fair':'drop'}">
											<img src="${data.key07>0?'assets/images/departmental-charge/arrow-up-filling.svg':data.key07==0?'assets/images/departmental-charge/icon-pingjun@2x.png':'assets/images/departmental-charge/arrow-down-filling.svg'}" >
											<span>${(data.key07 * 100).toFixed(1)}%</span>
										</div>
										<p>较上月</p>
									</div>
								</div>`)
	  }
	)
}

function getOverall(re) {
	console.log(re)
	let laikuanData = [];
	let yusuanData = [];
	let zhichuData = [];
	let legendData = ['本年来款', '本年预算', '本年支出'];
	let xData = [];
	for (let i = 0; i < re.length; i++) {
		xData.push(re[i].key01)
		laikuanData.push(FormatWYCash(re[i].key02));
		yusuanData.push(FormatWYCash(re[i].key03));
		zhichuData.push(FormatWYCash(re[i].key04));
	}
	
	let seriesData = [{
			name: "本年来款",
			barWidth: "20%",
			type: "bar",
			data: laikuanData,
			barGap: 0,
			itemStyle: {
				color: "#165DFF",
			}
		},
		{
			name: "本年预算",
			barWidth: "20%",
			type: "bar",
			barGap: 0,
			data: yusuanData,
			itemStyle: {
				color: "#F7BA1E",
			}
		},
		{
			name: "本年支出",
			barWidth: "20%",
			type: "bar",
			barGap: 0,
			data: zhichuData,
			itemStyle: {
				color: "#20C997",
			}
		}
	];
	have("overall-chart") ? monthTrend("overall-chart", seriesData, legendData, xData, 123) : "";
}


function getBasic(re) {
	let zhichuData = [];
	let legendData = [''];
	let xData = [];
	for (let i = 0; i < re.length; i++) {
		xData.push(re[i].key01)
		zhichuData.push(FormatWYCash(re[i].key02));
	}
	
	let seriesData = [{
		name: "支出",
		barWidth: "50%",
		type: "bar",
		data: zhichuData,
		itemStyle: {
			color: "#20C997",
		}
	}];
	have("basic-chart") ? monthTrend("basic-chart", seriesData, legendData, xData, 123) : "";
}


function getExpend(re) {
	let zhichuData = [];
	let legendData = [''];
	let xData = [];
	for (let i = 0; i < re.length; i++) {
		xData.push(re[i].key01)
		zhichuData.push(FormatWYCash(re[i].key02));
	}
	
	let seriesData = [{
		name: "支出",
		barWidth: "50%",
		type: "bar",
		data: zhichuData,
		itemStyle: {
			color: "#20C997",
		}
	}];
	have("expend-chart") ? monthTrend("expend-chart", seriesData, legendData, xData, 123) : "";
}


function getSchedule(data) {
	let $table = $('#schedule');
	$table.bootstrapTable('destroy');
	$('.container .box.box5').width($('.container .content .wrapper').width() - 52)
	$table.bootstrapTable({
		// url: 'http://127.0.0.1:8848/cl/assets/plugins/treegrid.json',
		data:data,
		height: 400,
		idField: 'id',
		showColumns: false,
		fixedColumns: true,
		fixedRightNumber: 2,
		columns: [
			[{
					field: 'key02',
					title: '项目',
					rowspan: 2,
					valign: 'middle',
					width: 150,
					formatter: blodName,
				},
				{
					field: 'key03',
					title: '科目编号',
					rowspan: 2,
					sortable: true,
					valign: 'middle',
					width: 120,
				},
				{
					field: 'key04',
					title: '年初结余',
					rowspan: 2,
					sortable: true,
					align: 'right',
					valign: 'middle',
					width: 120,
					formatter:wyText,
				},
				{
					field: 'key05',
					title: '本年预算',
					rowspan: 2,
					sortable: true,
					align: 'right',
					valign: 'middle',
					formatter:wyText,
					width: 120,
				},
				{
					field: 'key06',
					title: '序时进度',
					rowspan: 2,
					sortable: true,
					align: 'right',
					valign: 'middle',
					width: 120,
					formatter:wyText,
				},
				{
					field: 'key07',
					title: '本年来款',
					rowspan: 2,
					sortable: true,
					align: 'right',
					valign: 'middle',
					width: 120,
					formatter:wyText,
				},
				{
					field: 'key08',
					title: '预算执行率',
					rowspan: 2,
					sortable: true,
					align: 'right',
					valign: 'middle',
					width: 180,
					formatter: progress
				},
				{
					field: 'key09',
					title: '来款支出比例',
					rowspan: 2,
					sortable: true,
					align: 'right',
					valign: 'middle',
					width: 120,
					formatter:wyText,
				},
				{
					// field: 'name',
					title: '本年支出',
					colspan: 3,
					align: 'center',
					width: 420,
				},
				{
					field: 'key13',
					title: '结余合计',
					rowspan: 2,
					sortable: true,
					align: 'right',
					valign: 'middle',
					width: 150,
					formatter: blodText
				},
				{
					field: 'key14',
					title: '可用资金结余',
					rowspan: 2,
					sortable: true,
					align: 'right',
					valign: 'middle',
					width: 150,
					formatter: blodText
				},
			],
			[{
					field: 'key10',
					title: '上年结余支出',
					sortable: true,
					align: 'right',
					formatter:wyText,
				},
				{
					field: 'key11',
					title: '本年预算支出',
					sortable: true,
					align: 'right',
					formatter:wyText,
				},
				{
					field: 'key12',
					title: '小计',
					sortable: true,
					align: 'right',
					formatter:wyText,
				}
			]

		],
		treeShowField: 'key02',
		parentIdField: 'pid',
		onPostBody: function() {
			var columns = $table.bootstrapTable('getOptions').columns
			if (columns && columns[0][1].visible) {
				$table.treegrid({
					treeColumn: 0,
					onChange: function() {
						$table.bootstrapTable('resetView')
					}
				})
			}
		}
	})
}

function wyText(value){
	return `<p>${FormatWYCash(value)}</p>`
}
function blodText(value) {
	return `<h6>${FormatWYCash(value)}</h6>`
}
function blodName(value){
	return `<h6>${value}</h6>`
}
function progress(value) {
	return `<div class="process_container">
								<div>
								<div class="process_bar">
									<div class="${parseFloat(value)>1?'process exceed':'process'}" style="width:${value*72}px"></div>
								</div>
								<p>${(value * 100).toFixed(2)} %</p>
								</div>
							</div>`
}
